package com.ybkj.daijia.server.event.listener;

import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.config.SalesApi;
import com.ybkj.daijia.server.event.model.ActivityGiftCouponEvent;
import com.ybkj.daijia.server.event.model.MemberNoticeEvent;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.model.MemberNotice;
import com.ybkj.daijia.server.model.NoticeXPassenger;
import com.ybkj.daijia.server.service.ActivityService;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.CouponRuleService;
import com.ybkj.daijia.server.service.MemberNoticeService;
import com.ybkj.daijia.server.service.MemberService;
import com.ybkj.daijia.server.service.NoticeXPassengerService;
import com.ybkj.daijia.server.service.SmsService;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class ActivityGiftCouponEventListener implements
    ApplicationListener<ActivityGiftCouponEvent> {

    private static final Logger logger = LoggerFactory
        .getLogger(ActivityGiftCouponEventListener.class);

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private MemberNoticeService memberNoticeService;

    @Autowired
    private NoticeXPassengerService noticeXPassengerService;

    @Autowired
    private ApplicationEventMulticaster applicationEventMulticaster;

    @Autowired
    private SmsService smsService;

    @Autowired
    private ActivityService activityService;

    @Autowired
    private CouponRuleService couponRuleService;

    @Autowired
    private SalesApi salesApi;

    @Autowired
    private MemberService memberService;

    @Override
    @Async
    public void onApplicationEvent(ActivityGiftCouponEvent event) {
        logger.debug("member ActivityGiftCouponEvent event:{}", "");

        String memo = (String) event.getSource();
        List<Passenger> passengers = event.getPassengers();

        if (!CollectionUtils.isEmpty(passengers)) {
            for (Passenger passenger : passengers) {
                try {
                    MemberNotice memberNotice = new MemberNotice();
                    memberNotice.setCompanyId(passenger.getCompanyId());
                    memberNotice.setContent(memo);
                    memberNotice.setOperating("系统");
                    memberNotice.setPassengerIds("," + passenger.getId() + ",");
                    memberNoticeService.insertRecord(memberNotice);

                    NoticeXPassenger noticeXPassenger = new NoticeXPassenger();
                    noticeXPassenger.setPassengerId(passenger.getId());
                    noticeXPassenger.setNoticeId(memberNotice.getId());
                    noticeXPassenger.setHasRead(false);
                    noticeXPassengerService.save(noticeXPassenger);

                    List<Passenger> passengerList = new ArrayList<Passenger>();
                    passengerList.add(passenger);

                    MemberNoticeEvent memberNoticeEvent = new MemberNoticeEvent(memberNotice,
                        passengerList);
                    applicationEventMulticaster.multicastEvent(memberNoticeEvent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }


    }

}
